﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Plumber.Sales.ModuleData;
using Plumber.Masters.ModuleData;

namespace Plumber.Sales.Forms
{
    public partial class CreditNote : SalesInvoice
    {
        DRSalesInvoices drSaleInvoice;
        double originalPayed;

        public CreditNote()
            : base(global::InvoiceType.E_CreditNoteEm)
        {
            InitializeComponent();
            
            Label19.Visible = true;
            FncSalesNoRef.Visible = true;
        }

        protected override void OnAttachData()
        {
            base.OnAttachData();

            drSaleInvoice = new DRSalesInvoices(true);
        }

        protected override void OnPrepareAuxData()
        {
            base.OnPrepareAuxData();

            originalPayed = NtbTotal.Double;
        }

        protected override void OnCustomizeToolbar(ToolStrip toolstrip)
        {
            AddCustomButton("Invoice", Properties.Resources.T_SaleInvoice, Properties.Resources.B_Receipt, true);
        }

        protected override void OnCustomButtonClick(ToolStripButton button)
        {
            if (button.Name == "Invoice")
            {
                using (LoadInvoices lq = new LoadInvoices())
                {
                    OpenNewForm(lq, true);
                    if (lq.DialogResult == System.Windows.Forms.DialogResult.OK)
                        LoadInvoice(lq.List);
                }
            }
        }

        private void LoadInvoice(ListView.ListViewItemCollection list)
        {
            foreach (ListViewItem lvi in list)
            {
                if (lvi.Selected)
                {
                    LoadInvoiceInCreditNote(lvi.SubItems[0].Text);
                    break;
                }
            }
        }

        private void LoadInvoiceInCreditNote(string invoiceNo)
        {
            DRSalesInvoices drInvoice = new DRSalesInvoices(false);
            if (drInvoice.Find(global::InvoiceType.E_InvoiceEm, invoiceNo))
            {
                CbbType.SelectedValue = drInvoice.GetValue<string>(PL_SalesInvoices.Type);
                RdrCode.Text = drInvoice.GetValue<string>(PL_SalesInvoices.Master);

                TxtAddress.Text = drInvoice.GetValue<string>(PL_SalesInvoices.Address);
                TxtCity.Text = drInvoice.GetValue<string>(PL_SalesInvoices.City);
                TxtCounty.Text = drInvoice.GetValue<string>(PL_SalesInvoices.County);
                MtbZip.Text = drInvoice.GetValue<string>(PL_SalesInvoices.ZipCode);
                MtbTaxNo.Text = drInvoice.GetValue<string>(PL_SalesInvoices.TaxNo);
                MtbFiscalCode.Text = drInvoice.GetValue<string>(PL_SalesInvoices.FiscalCode);
                RdrAdmin.Text = drInvoice.GetValue<string>(PL_SalesInvoices.Administrator);
                RdrTitle.Text = drInvoice.GetValue<string>(PL_SalesInvoices.Title);

                RtbBody.Rtf = drInvoice.GetValue<string>(PL_SalesInvoices.InvoiceBody);

                DrMaster.Find(CbbType.SelectedValue.ToString(), RdrCode.Text);
                CbbRestraint.SelectedValue = DrMaster.GetValue<string>(PL_Masters.RestraintCode);
                CbbTaxCode.SelectedValue = DrMaster.GetValue<string>(PL_Masters.TaxCode);
                CkbRestraint.Checked = DrMaster.GetValue<bool>(PL_Masters.HasRestraint);

                FncSalesNoRef.Text = invoiceNo;
            }
        }

        protected override bool OnBeforeDelete()
        {
            if (!base.OnBeforeDelete())
                return false;

            if (drSaleInvoice.Find(global::InvoiceType.E_InvoiceEm, FncSalesNoRef.Text))
            {
                double val = drSaleInvoice.GetValue<double>(PL_SalesInvoices.AmountPayed) + drSaleInvoice.GetValue<double>(PL_SalesInvoices.CreditNoteAmount) - originalPayed;

                drSaleInvoice.SetValue<double>(PL_SalesInvoices.AmountPayed, val);
                bool closed = drSaleInvoice.GetValue<double>(PL_SalesInvoices.Total) <= val;
                drSaleInvoice.SetValue<bool>(PL_SalesInvoices.SalePayed, closed);
                if (!closed)
                    drSaleInvoice.SetValue<DateTime>(PL_SalesInvoices.PaymentDate, PL_SalesInvoices.EmptyDate);
                drSaleInvoice.Update();
            }
            return true;
        }

        protected override bool OnBeforeSave()
        {
            if (!base.OnBeforeSave())
                return false;

            if (drSaleInvoice.Find(global::InvoiceType.E_InvoiceEm, FncSalesNoRef.Text))
            {
                double val = drSaleInvoice.GetValue<double>(PL_SalesInvoices.AmountPayed) + NtbTotal.Double - originalPayed;
                if (val > drSaleInvoice.GetValue<double>(PL_SalesInvoices.Total))
                {
                    CreditNoteAmount.Value = val - drSaleInvoice.GetValue<double>(PL_SalesInvoices.Total);
                    val = drSaleInvoice.GetValue<double>(PL_SalesInvoices.Total);
                }
                drSaleInvoice.SetValue<double>(PL_SalesInvoices.AmountPayed, val);
                bool closed = drSaleInvoice.GetValue<double>(PL_SalesInvoices.Total) <= val;
                drSaleInvoice.SetValue<bool>(PL_SalesInvoices.SalePayed, closed);
                drSaleInvoice.SetValue<DateTime>(PL_SalesInvoices.PaymentDate, closed
                                                        ? CtbDate.Today
                                                        : PL_SalesInvoices.EmptyDate);
                drSaleInvoice.Update();
            }
            return true;
        }
    }
}
